package com.example.xgakj.newterminal.ui.activities;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.xgakj.mylibrary.mvp.utils.DensityUtils;
import com.example.xgakj.mylibrary.mvp.utils.TimeUtil;
import com.example.xgakj.mylibrary.mvp.utils.ToastUtil;
import com.example.xgakj.newterminal.R;
import com.example.xgakj.newterminal.base.BaseActivity;
import com.example.xgakj.newterminal.base.Parameters;
import com.example.xgakj.newterminal.utils.FileUtil;
import com.example.xgakj.newterminal.views.ViewWrapper;
import com.guanaj.easyswipemenulibrary.EasySwipeMenuLayout;
import com.qmuiteam.qmui.util.QMUIViewHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.socks.library.KLog;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VideoRecordListActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    private List<String> files = new ArrayList<>();
    String path = Parameters.VIDEO_PATH;
    private QuickAdapter quickAdapter;
    private Button btnShow;
    @BindView(R.id.tv_start)
    TextView tvStart;
    @BindView(R.id.tv_end)
    TextView tvEnd;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    private ViewWrapper viewWrapper;
    private boolean isShowingSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_record_list);
        ButterKnife.bind(this);
        initTopbar();
        initViews();

    }

    private void initTopbar() {
        topbar.setTitle("视频记录");
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        btnShow = topbar.addRightTextButton("搜索", QMUIViewHelper.generateViewId());
        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShowingSearch){
                    btnShow.setText("搜索");
                    dismissSearchView();
                }else {
                    btnShow.setText("收起");
                    showSearchView();
                }
            }
        });
    }
    private void showSearchView() {
        isShowingSearch=true;
        viewWrapper = new ViewWrapper(llSearch);
        ObjectAnimator.ofInt(viewWrapper, "height", DensityUtils.dip2px(mContex, 56)).setDuration(300).start();

    }
    private void dismissSearchView() {
        isShowingSearch=false;
        if (viewWrapper==null){
            return;
        }
        ObjectAnimator.ofInt(viewWrapper, "height", DensityUtils.dip2px(mContex, 0)).setDuration(300).start();

    }

    private void initViews() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        files = FileUtil.getFiles(path);
        quickAdapter = new QuickAdapter();
        quickAdapter.openLoadAnimation();
     /*   quickAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });*/
        recyclerView.setAdapter(quickAdapter);
    }

    public class QuickAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
        public QuickAdapter() {
            super(R.layout.item_set_list_delete, files);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final String item) {
            String name = item.substring(item.lastIndexOf("/") + 1);
            helper.setText(R.id.tv_name, name);
            helper.getView(R.id.content).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //判断后缀是h.264还是mp4
                    String suffix=item.substring(item.lastIndexOf(".")+1);
                    KLog.e(suffix);
                    Intent intent;
                    if (suffix.equals("h264")){
                        intent = new Intent(VideoRecordListActivity.this, VedioPlayActivity.class);
                        intent.putExtra("path", item);
                        startActivity(intent);
                    }else if (suffix.equals("mp4")){
                        intent = new Intent(VideoRecordListActivity.this, Playmp4Activity.class);
                        intent.putExtra("path", item);
                        startActivity(intent);
                    }else {
                        ToastUtil.showShort(VideoRecordListActivity.this,"视频格式不对，无法播放");
                    }


                }
            });
            helper.getView(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    KLog.e(item);
                    File file = new File(item);
                    if (file.exists()) {
                        file.delete();
                        KLog.e(item + "删除");
                    }
                    EasySwipeMenuLayout easySwipeMenuLayout = helper.getView(R.id.es);
                    easySwipeMenuLayout.resetStatus();
                    int position = helper.getLayoutPosition();
                    int size = getData().size();
                    getData().remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position, size - position - 1);

                }
            });

        }
    }
    @OnClick({R.id.tv_start, R.id.tv_end, R.id.iv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_start:
                TimePickerView startTimeView = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        tvStart.setText(TimeUtil.Date2String(date));
                    }
                }).setType(new boolean[]{true, true, true, false, false, false})
                        .build();
                startTimeView.show();
                break;
            case R.id.tv_end:
                TimePickerView endTimeView = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        tvEnd.setText(TimeUtil.Date2String(date));
                    }
                }).setType(new boolean[]{true, true, true, false, false, false})
                        .build();
                endTimeView.show();
                break;
            case R.id.iv_search:
                String start_value = tvStart.getText().toString().trim();
                String end_value = tvEnd.getText().toString().trim();
                long start= TimeUtil.String2long(start_value);
                long end= TimeUtil.String2long(end_value);
                if (start==0||end==0||start>end){
                    ToastUtil.showShort(mContex,"请选择正确的时间段查询");
                    return;
                }
                List<String> list = FileUtil.selectFilesByTime(Parameters.VIDEO_PATH, ".mp4", ".h264", start, end,"yyyy_MM_dd_HH_mm_ss");
                if (list!=null){
                    KLog.e(list.size());
                    files.clear();
                    files.addAll(list);
                    quickAdapter.notifyDataSetChanged();
                    btnShow.setText("搜索");
                    dismissSearchView();
                }
                break;
            default:
        }
    }

}
